package com.zlkj.pro.modular.business.controller;

import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlkj.pro.modular.admin.entity.Config;
import com.zlkj.pro.modular.admin.service.ConfigService;
import com.zlkj.pro.modular.business.model.dto.AgentDTO;
import com.zlkj.pro.modular.business.model.vo.AgentVO;
import com.zlkj.pro.modular.business.service.AgentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

import static com.zlkj.pro.core.consts.ProjectConstants.SERVICE_API_PREFIX;

/**
 * @author liyang
 * @date 2024/4/6
 * @time 14:34
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@ApiResource(name = "代理商信息", path = SERVICE_API_PREFIX + "/agent")
public class AgentController {

    private final AgentService agentService;

    private final ConfigService configService;

    @GetResource(name = "分页查询", path = "/findPage")
    public ResponseData findPage(AgentDTO dto) {
        PageResult<AgentVO> restData = agentService.findPage(dto);
        return new SuccessResponseData(restData);
    }

    @GetResource(name = "查询列表", path = "/findList")
    public ResponseData findList(AgentDTO dto) {
        List<AgentVO> restData = agentService.findList(dto);
        return new SuccessResponseData(restData);
    }

    @GetResource(name = "查询详情", path = "/findDetail")
    public ResponseData findDetail(AgentDTO dto) {
        AgentVO restData = agentService.findDetail(dto);
        return new SuccessResponseData(restData);
    }

    @PostResource(name = "添加", path = "/add")
    public ResponseData add(@RequestBody AgentDTO dto) {
        boolean restData = agentService.add(dto);
        return new SuccessResponseData(restData);
    }

    @PostResource(name = "编辑", path = "/edit")
    public ResponseData edit(@RequestBody AgentDTO dto) {
        boolean restData = agentService.edit(dto);
        return new SuccessResponseData(restData);
    }

    @PostResource(name = "删除", path = "/delete")
    public ResponseData delete(@RequestBody AgentDTO dto) {
        boolean restData = agentService.delete(dto);
        return new SuccessResponseData(restData);
    }

    @PostResource(name = "批量删除", path = "/batchDelete")
    public ResponseData batchDelete(@RequestBody List<Long> ids) {
        boolean restData = agentService.batchDelete(ids);
        return new SuccessResponseData(restData);
    }

    @GetResource(name = "获取地域信息", path = "/getRegions")
    public ResponseData getRegions() {
        LambdaQueryWrapper<Config> queryWrapper = new LambdaQueryWrapper<Config>();
        queryWrapper
                .eq(Config::getCode, "REGION")
                .eq(Config::getDelFlag, YesOrNotEnum.N.getCode());
        Config config = configService.getOne(queryWrapper);
        List<String> regions = Arrays.asList(config.getValue().split(","));
        return new SuccessResponseData(regions);
    }
}
